package com.his.modules.sysjob.job;

import com.his.modules.business.entity.MedicalRecord;
import com.his.modules.business.entity.MedicalRecordAnalysis;
import com.his.modules.business.entity.MedicalRecordAnalysisRecord;
import com.his.modules.business.enums.BlflEnum;
import com.his.modules.business.service.MedicalRecordService;
import com.his.modules.business.utils.MedicalRecordAnalysisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Component("analysisZyMedicalRecordJob")
@Slf4j
public class AnalysisZyMedicalRecordJob {
    @Autowired
    private MedicalRecordService medicalRecordService;
    /**
     * 解析住院病历
     * @return
     */
    public void analysisZyMedicalRecordJob() {
        log.info("解析住院病历定时任务开始执行=================");
        MedicalRecord medicalRecord = new MedicalRecord();
        medicalRecord.setBlfl(BlflEnum.ZY.getCode());
        List<MedicalRecord> medicalRecordList = medicalRecordService.medicalRecordList(medicalRecord);

        if(!CollectionUtils.isEmpty(medicalRecordList)) {
            System.out.println("解析住院病历定时任务开始执行=================获取病历数：" + medicalRecordList.size());
            for(MedicalRecord mr : medicalRecordList) {
                log.info("开始解析病历{}----{}=================", mr.getBlbh(), mr.getBlmc());
                try {
                    String blnr = new String(mr.getBlnr(), "GBK");
                    MedicalRecordAnalysis medicalRecordAnalysis = new MedicalRecordAnalysis();
                    medicalRecordAnalysis.setBlbh(mr.getBlbh());
                    medicalRecordAnalysis.setBlfl(mr.getBlfl());
                    medicalRecordAnalysis.setBlmc(mr.getBlmc());
                    medicalRecordAnalysis.setJxzt("1");
                    List<MedicalRecordAnalysisRecord> medicalRecordAnalysisRecordList = MedicalRecordAnalysisUtil.medicalRecordAnalysis(blnr, medicalRecordAnalysis);
                    if(!CollectionUtils.isEmpty(medicalRecordAnalysisRecordList)) {
                        for(MedicalRecordAnalysisRecord medicalRecordAnalysisRecord : medicalRecordAnalysisRecordList) {
                            medicalRecordAnalysisRecord.setBlbh(mr.getBlbh());
                            medicalRecordAnalysisRecord.setBllx(mr.getBllx());
                            medicalRecordAnalysisRecord.setBllb(mr.getBllb());
                            medicalRecordAnalysisRecord.setMblb(mr.getMblb());
                            medicalRecordAnalysisRecord.setMbbh(mr.getMbbh());
                            medicalRecordAnalysisRecord.setJzxh(mr.getJzxh());
                        }
                    }
                    //保存解决数据
                    medicalRecordService.medicalRecordAnalysis(medicalRecordAnalysis, medicalRecordAnalysisRecordList);
                } catch (UnsupportedEncodingException e) {
                    log.info("解析出错了！" + e.getMessage());
                }
            }
        }
    }
}
